/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2022-12-20 15:31:56
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-08-23 17:14:44
 * @FilePath: \cxl-admin\src\pages\system\menu\list\index.ts
 */
import { enAble, menuDelete, menuList, permissionDelete, permissionEnable } from '@/apis/system/menu'
import { searchItemInterface } from '@/components/search'
import { bubbleSort } from '@/store/router.store'
import { handleTree } from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus'
import { reactive } from 'vue'

export class Menu {
    //弹窗数据
    dialog: any = reactive({
        visible: false, //弹窗显示隐藏
        type: 'add', //弹窗类型 修改 、 新增
        id: '', //对应菜单id
        menuType: '1',
        menuName: '',
        menuData: null
    })

    //表格数据
    tableData: any[] = reactive([])
    menuArray: any[] = []

    //搜索字段
    searchList: Array<searchItemInterface | string> = [{ key: 'name', type: 'text', name: '菜单名称' }]

    constructor() {
        this.getData()
    }

    /**
     * 监听点击搜索
     */
    onSearch = (res: any) => {
        this.getData(res.name)
    }

    /**
     * 监听点击重置按钮
     */
    onReset = () => {
        this.getData()
    }

    /**
     * 删除 id 对应 菜单
     * @param id
     */
    removeMenu = (id: string) => {
        ElMessageBox.confirm('您确定要删除该菜单吗?', '温馨提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        })
            .then(() => {
                ElMessage({
                    type: 'success',
                    message: '操作成功'
                })
            })
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: '取消操作'
                })
            })
    }

    /**
     * 打开弹窗编辑对应id的菜单信息
     * @param type
     * @param id
     */
    openDialog = (type: string, menuType: number, row?: any) => {
        this.dialog.type = type
        this.dialog.visible = true
        this.dialog.menuType = menuType + ''
        if (type === 'create') {
            //新增菜单
            if (row) {
                //存在id
                this.dialog.menuName = row.name
                //console.log('row', row)
                if (row.parentId) {
                    //二级菜单
                    this.dialog.menuType = 3 + ''
                } else {
                    //一级菜单
                }
                this.dialog.data = row
            } else {
                //不存在id
                this.dialog.menuName = ''
                this.dialog.data = null
            }
        } else if (type === 'edit') {
            //编辑菜单
            if (row.type === 2) {
                //权限
                this.menuArray.forEach(item => {
                    if (item.id == row.parentId) {
                        this.dialog.menuName = item.name
                    }
                })
                this.dialog.menuType = 3 + ''
            } else {
                //菜单
                if (row.parentId) {
                    //二级菜单
                    this.dialog.menuName = row.name
                } else {
                    //一级菜单
                    this.dialog.menuName = ''
                }
            }
            this.dialog.data = row
        }
    }

    //删除菜单
    onDeleteMenu = (row: any) => {
        //console.log(row.type)
        ElMessageBox.confirm('您确定要删除该菜单吗?', '温馨提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        })
            .then(() => {
                /**
                 * 只要被禁用，都可以删除
                 */
                if (!row.status) {
                    if (row.type === 1) {
                        menuDelete(row.id).then(res => {
                            if (res.data.value === 200) {
                                ElMessage.success('菜单已删除！')
                                this.getData()
                            } else {
                                ElMessage.error(res.data.desc)
                            }
                        })
                    } else {
                        permissionDelete(row.id).then(res => {
                            if (res.data.value === 200) {
                                ElMessage.success('权限已删除！')
                                this.getData()
                            } else {
                                ElMessage.error(res.data.desc)
                            }
                        })
                    }
                }
            })
            .catch(() => {
                // ElMessage({
                //     type: 'info',
                //     message: '取消操作'
                // })
            })
    }

    /**
     * 监听弹窗隐藏
     */
    onDialogClose() {
        this.dialog.visible = false
        this.getData()
    }

    /**
     * 获取数据
     * @param name
     */
    getData = (name = '') => {
        menuList(name).then(res => {
            const arr: any[] = JSON.parse(JSON.stringify(res.data.data))
            arr.forEach(item => {
                item.status = item.status == 1 ? true : false
            })
            this.menuArray.length = 0
            this.menuArray.push(...arr)
            this.tableData.length = 0
            const list: any[] = bubbleSort(handleTree(arr, 'id', 'parentId'))
            this.tableData.push(...list)
        })
    }
    setEnable = (id: string, status: number, isMenu = true) => {
        if (isMenu) {
            enAble(id, status).then(res => {
                if (res.data.value === 200) {
                    ElMessage.success('菜单已更新，请刷新页面')
                    this.getData()
                } else {
                    ElMessage.error(res.data.desc)
                }
            })
        } else {
            permissionEnable(id, status).then(res => {
                if (res.data.value === 200) {
                    ElMessage.success('菜单已更新，请刷新页面')
                    this.getData()
                } else {
                    ElMessage.error(res.data.desc)
                }
            })
        }
    }
    /**
     * 监听点击改变菜单状态
     * @param id
     * @param status
     */
    onMenuStatusChange = (row: any) => {
        //console.log(row)
        const id = row.id
        const status = Number(row.status)

        //一级菜单已经更新，子菜单不可操作
        //一级菜单禁用，要判断二级菜单是否全部禁用
        //子级菜单启用，要判断父级菜单是否启用

        let isDisabled = false
        //console.log(this.menuArray)
        if (row.parentId === '') {
            //一级菜单
            if (status) {
                this.setEnable(id, status)
            } else {
                //禁用需要判断二级菜单是否有激活的
                let isActive = false
                for (let i = 0; i < row.children.length; i++) {
                    if (row.children[i].status) {
                        isActive = true
                        break
                    }
                }

                if (isActive) {
                    //子菜单有激活状态
                    //console.log(status, isActive, '不可禁用')
                    row.status = true
                    ElMessage.error('存在启用的子菜单，不可禁用！')
                } else {
                    //子菜单全部是禁用状态
                    //console.log(status, isActive, '可禁用')
                    this.setEnable(id, status)
                }
            }
        } else {
            //二级菜单 | 按钮
            for (let i = 0; i < this.menuArray.length; i++) {
                if (this.menuArray[i].id === row.parentId) {
                    isDisabled = this.menuArray[i].status
                    //console.log(this.menuArray[i])
                    break
                }
            }
            if (isDisabled) {
                //父级菜单已启用
                if (status) {
                    //启用
                    if (row.type === 1) {
                        //二级菜单
                        this.setEnable(id, status)
                    } else if (row.type === 2) {
                        //按钮
                        this.setEnable(id, status, false)
                    }
                } else {
                    //禁用
                    let isActive = false
                    if (row.children) {
                        for (let i = 0; i < row.children.length; i++) {
                            if (row.children[i].status) {
                                isActive = true
                                break
                            }
                        }
                    }

                    //console.log(status, isActive, this.menuArray)
                    if (isActive) {
                        ElMessage.error('存在启用的子菜单，不可禁用！')
                        row.status = true
                    } else {
                        if (row.type === 1) {
                            //二级菜单
                            this.setEnable(id, status)
                        } else if (row.type === 2) {
                            //按钮
                            this.setEnable(id, status, false)
                        }
                    }
                }
            } else {
                ElMessage.error('父级菜单未启用！')
                row.status = false
            }
        }
    }
}
